package com.example.sm.mycook.task;

import android.text.TextUtils;
import android.util.Log;

import com.example.sm.mycook.beans.HomeMyRVBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */
public class ParseJsonHome {

    public static  List<HomeMyRVBean> parseHomeData(String jsonString)
    {
         List<HomeMyRVBean> list=new ArrayList<>();

        try {

            JSONObject jsonObject=new JSONObject(jsonString);

            JSONObject jsonObject1=jsonObject.getJSONObject("data");

            JSONArray jsonArray=jsonObject1.getJSONArray("acts");
//            Log.e("AAAA","解析的时候，acts数组的长度是：====》"+jsonArray.length());

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject jsonObject2=jsonArray.getJSONObject(i);


                String templateType=jsonObject2.getString("templateType");
//                Log.e("AAA","最外层的type是：====ssssssss======》"+templateType);


                    JSONArray jsonArray12=jsonObject2.getJSONArray("items");

                    for (int i1 = 0; i1 < jsonArray12.length(); i1++) {

                        JSONObject jsonObject32=jsonArray12.getJSONObject(i1);


                       if (templateType.equals("7")) {
                            String imgUrl=jsonObject32.getString("imgUrl");
                            String type=jsonObject32.getString("type");
                            String source=jsonObject32.getString("source");
                            String pos=jsonObject32.getString("pos");
                            String hashKey=jsonObject32.getString("$$hashKey");
                            String goodsName=jsonObject32.getString("goodsName");
                            String price=jsonObject32.getString("price");
                            String unit=jsonObject32.getString("unit");
                            String marketPrice=jsonObject32.getString("marketPrice");
                            HomeMyRVBean homeMyRVBean=new HomeMyRVBean(templateType,goodsName,hashKey,imgUrl,marketPrice,pos,price,source,type,unit);
                            list.add(homeMyRVBean);

                       }else {
                           String imgUrl=jsonObject32.getString("imgUrl");
                        String type=jsonObject32.getString("type");
                        String source=jsonObject32.getString("source");
                        String pos=jsonObject32.getString("pos");
                        String hashKey=jsonObject32.getString("$$hashKey");
                        String goodsName="补充";
                        String price="补充";
                        String unit="补充";
                        String marketPrice="补充";
//                        Log.e("AAA","补充，，，，imgeUrl==="+imgUrl+"type==="+type+"temTyop==="+templateType);

                        HomeMyRVBean homeMyRVBean=new HomeMyRVBean(templateType,goodsName,hashKey,imgUrl,marketPrice,pos,price,source,type,unit);
                        list.add(homeMyRVBean);

                       }
                    }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


        return list;
    }
}
